//
//  DriveCodeViewController.m
//  CarCare
//
//  Created by ileo on 14-9-19.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "DriveCodeViewController.h"
#import "MyCarInfoInputViewController.h"
#import "NetCar.h"
#import "UserHelper.h"
#import "ScanViewController.h"
#import "OBDViewController.h"

@interface DriveCodeViewController ()

@property (nonatomic, strong) UITextField *deviceTextField;
@property (nonatomic, strong) UITextField *deviceCodeTextField;

@property (nonatomic, strong) UIButton *nextButton;

@end

@implementation DriveCodeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"绑定设备号";
    
    self.deviceTextField = [self textFieldWithFrame:FactoryRectDefault(TOP_SIDE) type:TEXTFIELD_Default_Num];
    self.deviceTextField.text = USERINFO.pastDevice;
    self.deviceTextField.placeholder = LOCSTRING(@"fill_devide_imei");
    
    __weak __typeof(self) wself = self;
    [ViewFactory addButtonType:BUTTON_DEVICE_CODE onTextField:self.deviceTextField inView:self.view action:^{
        ScanViewController *vc = [[ScanViewController alloc] init];
        
        [wself presentViewController:[UINavigationController navigationControllerWithRootViewController:vc] animated:YES completion:nil willDismissCallback:nil didDismissCallback:^(BOOL success, id info) {
            wself.deviceTextField.text = USERINFO.device;
        }];
        
    } ok:nil];
    
    self.deviceCodeTextField = [self textFieldWithFrame:FactoryRectDefault(self.deviceTextField.bottom + GAP_HEIGHT) type:TEXTFIELD_Default_Num];
    self.deviceCodeTextField.text = USERINFO.deviceCode;
    self.deviceCodeTextField.placeholder = LOCSTRING(@"fill_devide_code");
    
    self.nextButton = [ViewFactory normalButtonWithFrame:FactoryRectButtonDefault(__MAIN_HEIGHT_64 - BOTTOM_SIDE - 30) title:@"下一步" inView:self.view action:^{
        [wself clickOnNext];
    }];
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"暂时不绑定"];
    [attr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, 5)];
    [attr addAttribute:NSForegroundColorAttributeName value:[Theme mainColor] range:NSMakeRange(0, 5)];
    UILabel *label = [Factory labelWithFrame:CGRectMake(0, self.nextButton.bottom + 10, __MAIN_WIDTH, 30) font:FONT_DEFAULT_Light(14) text:nil textColor:nil onView:self.view textAlignment:NSTextAlignmentCenter];
    label.attributedText = attr;

    [[UIButton buttonEmptyWithFrame:label.frame click:^{
        [SCREEN showMainScreen];
    }] setupOnView:self.view];
     

}
- (NSInteger)textFieldMaxNumberWithTextField:(UITextField *)textField
{
    if (textField == self.deviceTextField) {
        return 14;
    }
    else if(textField == self.deviceCodeTextField  )
    {
        return 3;
    }
    else{
        return [super textFieldMaxNumberWithTextField:textField];
    }

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)clickOnNext{

    MyCarInfoInputViewController *vc = [[MyCarInfoInputViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW carModel:nil];
    [vc resetBindingClickDone:^{
        OBDViewController *obd = [[[OBDViewController alloc] init] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
        [vc.navigationController pushViewController:obd animated:YES];
    }];
    
    if ([USERINFO.pastDevice isEqualToString:self.deviceTextField.text] && [USERINFO.deviceCode isEqualToString:self.deviceCodeTextField.text] && USERINFO.vehicleId != 0) {
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        if ([Check isDevice:self.deviceTextField.text] && [Check isDeviceCode:self.deviceCodeTextField.text]) {
            __weak __typeof(self) wself = self;
            [[[NetCar bindingDevice:self.deviceTextField.text deviceCode:self.deviceCodeTextField.text] requestWithLoad:RequestLoadShowLoading] requestSuccess:^(id JSON) {
                USERINFO.device = wself.deviceTextField.text;
                USERINFO.pastDevice = wself.deviceTextField.text;
                USERINFO.deviceCode = wself.deviceCodeTextField.text;
                USERINFO.vehicleId = [JSON[@"vehicleId"] integerValue];
                
                if ([JSON[@"styleId"] integerValue] > 0) {
                    [UserHelper getUserInfoSuccess:^{
                        if (USERINFO.carStyleID > 0) {
                            USERINFO.isLogin = YES;
                            [SCREEN showMainScreen];
                        }else{
                            [wself.navigationController pushViewController:vc animated:YES];
                        }
                    } fail:^{
                        [wself.navigationController pushViewController:vc animated:YES];
                    }];
                }else{
                    [wself.navigationController pushViewController:vc animated:YES];
                }

            } failure:^(id JSON) {
                [AlertMgr showShortWarnTextTips:@[JSON[@"errMsg"]]];
            }];
            
        }
    }
    
}

#pragma mark - keyboard
-(void)moveViewWhenKeyBoardChangeToHeight:(CGFloat)height WithDuiation:(NSTimeInterval)duration{

    [UIView animateWithDuration:duration animations:^{
        self.nextButton.bottom = __MAIN_HEIGHT_64 - ((height == 0) ? ( BOTTOM_SIDE + 30) : height + 10);
    }];

}
@end
